package com.css.dzj.dzjap.modules.administration.punish.controller;

import com.css.aq.server.service.BaseService;
import com.css.dzj.dzjap.common.util.BeanUtil;
import com.css.dzj.dzjap.common.util.R;
import com.css.dzj.dzjap.modules.administration.check.entity.ApJgxwCheckAction;
import com.css.dzj.dzjap.modules.administration.punish.entity.Punish;
import com.css.dzj.dzjap.modules.administration.punish.service.PunishService;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/punish")
public class PunishController {

    //注入Service
    @Autowired
    private PunishService punishService;
    @Autowired
    BaseService baseService;

    //查询行政处罚行为信息表
    @GetMapping(value = "/getPunish")
    public R getPunish(@RequestParam("id") String id){
        baseService.logSuccess(3,"查询信息成功","2");
        return R.ok().put("data",punishService.getPunish(id));
    }


    // 添加行政处罚行为信息表
    @PostMapping(value = "/addPunish")
    public R addPunish(@RequestBody Punish push) {
        try{
            String flag = punishService.addPunish(push);
            if (!"0".equals(flag)) {
                R r = new R();
                r.put("code", 200);
                r.put("msg", "操作成功");
                r.ok().put("data", flag);
                baseService.logSuccess(3,"添加信息成功","2");
                return r;
            } else {
                baseService.logFailed(3,"添加信息失败","2");
                return R.error("添加失败");
            }
        } catch (Exception e){
            baseService.logFailed(3,"添加信息失败","2");
            return R.error(e.getMessage());
        }
    }

    // 修改行政处罚行为信息表
    @PostMapping(value = "/updatePunish")
    public R updatePunish(@RequestBody Punish push ){

        try {
            Punish aPush = punishService.getPunish(push.getId());
            BeanUtil.copyBeanNotNullToBean(push, aPush);
            punishService.update(aPush);
            baseService.logSuccess(3,"修改信息成功","2");
            return R.ok();
        } catch (Exception e) {
            baseService.logFailed(3,"修改信息失败","2");
            return R.error(e.getMessage());
        }
    }


    // 删除行政处罚行为信息表
    @PostMapping(value = "/deletePunish")
    public R deletePunish(@RequestParam("id") String id){
        if(punishService.deletePunish(id)){
            baseService.logSuccess(3,"删除信息成功","2");
            return R.ok("操作成功!");
        }else{
            baseService.logFailed(3,"删除信息失败","2");
            return  R.error("操作失败!");
        }
    }

    /**
     *分页查询所有行政处罚行为信息表
     */
    @GetMapping(value = "/getAllPunish")
    public R getAllPunish(@RequestParam(required = false) Integer pageNo,
                         @RequestParam(required = false) Integer pageSize
    ) {

        PageRequest pageRequest = PageRequest.of(pageNo-1, pageSize);
        baseService.logSuccess(3,"查询信息成功","2");
        return R.ok().put("data", punishService.getAllPunish(pageRequest));
    }

    /**
     * 多条件查询所有行政处罚行为信息表
     */
    @GetMapping(value ="/getAllConPunish")
    public R getAllConPunish(@RequestParam String recordUniqueIdentity,
                            @RequestParam(required = false) String superviseItemCode,
                            @RequestParam(required = false) String punishActionName,
                            @RequestParam(required = false) String punishActionCode,
                            @RequestParam(required = false) String implementInstitution,
                            @RequestParam(required = false) String implementInstitutionCode,
                            @RequestParam(required = false) Integer pageNo,
                            @RequestParam(required = false) Integer pageSize){

        PageRequest pageRequest = PageRequest.of(pageNo-1,pageSize);
        baseService.logSuccess(3,"查询信息成功","2");
        return R.ok().put("data", punishService.getAllConPunish(pageRequest,recordUniqueIdentity,superviseItemCode,punishActionName,punishActionCode,implementInstitution,implementInstitutionCode));
    }
    /***
     * 获取处罚行为
     * @return
     */
    @GetMapping("/punish")
    public R countPunish(){
        HashMap count=punishService.getCount();
        return R.ok().put("count",count);
    }
    @GetMapping("/punishyear")
    public R countPunishByYear(String year){
        HashMap count=punishService.getCountByYear(year);
        return R.ok().put("count",count);
    }
    /***
     * 获取当前登陆人的机构的处罚行为
     * @param request
     * @return
     */
    //@GetMapping("/getpunish")
    public R getOpPunish(HttpServletRequest request){
        Long count=punishService.getOpCheck(request);
        return R.ok().put("count",count);
    }

    /***
     * 多条件查询对外接口
     * @param superviseItemCode
     * @param punishActionName
     * @param caseSource
     * @param punishType
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping(value ="/getAll")
    public QueryResults<Punish> getAll(
                    @RequestParam(required = false) String superviseItemCode,
                    @RequestParam(required = false) String punishActionName,
                    @RequestParam(required = false) String caseSource,
                    @RequestParam(required = false) String punishType,
                    @RequestParam(required = false) Integer pageNo,
                    @RequestParam(required = false) Integer pageSize){

        PageRequest pageRequest = PageRequest.of(pageNo-1,pageSize);
        return punishService.getAll(pageRequest,superviseItemCode,punishActionName,caseSource,punishType);
    }

    /***
     * 查询列表
     * @param superviseItemCode
     * @param punishActionName
     * @param caseSource
     * @param punishType
     * @return
     */
    @GetMapping(value ="/getAllList")
    public List<Punish> getAll(
            @RequestParam(required = false) String superviseItemCode,
            @RequestParam(required = false) String punishActionName,
            @RequestParam(required = false) String caseSource,
            @RequestParam(required = false) String punishType){
        List<Punish> list=punishService.getAllList(superviseItemCode,punishActionName,caseSource,punishType);
        return list;
    }

    /**
     * 获取总数
     * @return
     */
    @GetMapping("/countNum")
    public Long getCount(){
       Long num=punishService.getNum();
       return num;
    }

    /***
     * 根据ids查询
     * @param ids
     * @return
     */
    @GetMapping("/getByPuIds")
    public List<Punish> getByIds(@RequestParam String ids){
        List<String> arr= Arrays.asList(ids.split(","));
        List<Punish> list=punishService.getByIds(arr);
        return list;
    }
    //查询行政处罚行为信息表
    @GetMapping(value = "/getPunishById")
    public R getPunishById(@RequestParam("id") String id){
        return R.ok().put("data",punishService.getPunish(id));
    }

}
